package com.dl.exam.servlet.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员类操作
 * @author DL
 * @create 2022-1-19 14:37:55
 */
public class AdminController extends HttpServlet {

/*
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String method = req.getParameter("method");
        //System.out.println("method = " + method);
        if (method != null && !method.equals("")){
            switch (method){
                case "list" :
                    System.out.println(this.getClass().getSimpleName() + " do list");
                    list(req, resp);
                    break;
                case "updateState" ://更新用户状态
                    System.out.println(this.getClass().getSimpleName() + " do updateState");
                    updateState(req, resp);
                    break;
                case "updateAllInfo" ://更新用户全部信息
                    System.out.println(this.getClass().getSimpleName() + " do updateAllInfo");
                    updateAllInfo(req, resp);
                    break;
                case "question_list" ://试题列表
                    System.out.println(this.getClass().getSimpleName() + " do question_list");
                    question_list(req, resp);
                    break;
                case "addSingle" ://增加单选题
                    System.out.println(this.getClass().getSimpleName() + " do addSingle");
                    addSingle(req, resp);
                    break;
                case "addSubjective" ://增加主观题
                    System.out.println(this.getClass().getSimpleName() + " do addSubjective");
                    addSubjective(req, resp);
                    break;
                case "search" ://根据 姓名 搜索用户
                    System.out.println(this.getClass().getSimpleName() + " do search");
                    searchUserByName(req, resp);
                    break;
                case "searchQuestion" ://根据 科目和难度 搜索试题
                    System.out.println(this.getClass().getSimpleName() + " do searchQuestion");
                    searchQuestionBySubject(req, resp);
                    break;
                case "queryUserAllInfo" ://根据 角色id 查询用户
                    System.out.println(this.getClass().getSimpleName() + " do queryUserAllInfo");
                    queryUserAllInfo(req, resp);
                    break;
                case "queryUserByRoleId" ://根据 角色id 查询用户
                    System.out.println(this.getClass().getSimpleName() + " do queryUserByRoleId");
                    queryUserByRoleId(req, resp);
                    break;
                case "axios" ://axios测试
                    System.out.println(this.getClass().getSimpleName() + " do axios");
                    axios(req, resp);
                    break;
                case "getSingle" :
                    getSingle(req, resp);
                    break;
                case "updateSingle" :
                    updateSingle(req, resp);
                    break;
                case "getSubjective" :
                    getSubjective(req, resp);
                    break;
                case "updateSubjective" :
                    updateSubjective(req, resp);
                    break;
                case "getAllExamByiId" :
                    getAllExamByiId(req, resp);
                    break;
                default :
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req, resp);
    }

    @Test
    public void aaa(){
    }

    UserServiceImpl userService = new UserServiceImpl();
    AdminServiceImpl adminService = new AdminServiceImpl();
    SingleServiceImpl singleService = new SingleServiceImpl();
    SubjectiveServiceImpl subjectiveService = new SubjectiveServiceImpl();



    //查询用户所有的考试
    private void getAllExamByiId(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String userId = req.getParameter("userId");
        String currentPage = req.getParameter("currentPage");
        Page<Exam> examPage = userService.queryAllExam(userId, Integer.parseInt(currentPage), 20);
        String string = JSONArray.toJSONString(examPage);
        System.out.println(this.getClass().getSimpleName() + " string = " + string);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
    }

    private void getSingle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String questionId = req.getParameter("question_id");
        SingleQuestion singleQuestion = singleService.getSingleQuestionById(questionId);
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(singleQuestion));
    }

    private void updateSingle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JSONObject parse = JsonUtils.parse(req);
        JSONObject form = parse.getJSONObject("singleDialogForm");
        System.out.println("form = " + form);
        String question_title = form.getString("question_title");
        String question_a = form.getString("question_a");
        String question_b = form.getString("question_b");
        String question_c = form.getString("question_c");
        String question_d = form.getString("question_d");
        String answer = form.getString("answer");
        String question_id = form.getString("question_id");
        boolean b = singleService.updateSingle(question_title, question_a, question_b, question_c, question_d, answer, question_id);
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(b));
    }

    private void getSubjective(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String questionId = req.getParameter("question_id");
        SubjectiveQuestion subjectiveQuestion = subjectiveService.getSubjectiveQuestionById(questionId);
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(subjectiveQuestion));
    }

    private void updateSubjective(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JSONObject parse = JsonUtils.parse(req);
        JSONObject form = parse.getJSONObject("singleDialogForm");
        System.out.println("form = " + form);
        String question_title = form.getString("question_title");
        String answer = form.getString("answer");
        String question_id = form.getString("question_id");
        boolean b = subjectiveService.updateSubjective(question_title, answer, question_id);
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(b));
    }


    private void updateAllInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JSONObject parse = JsonUtils.parse(req);
        JSONObject form = parse.getJSONObject("modifyAllInfoForm");
        System.out.println("form = " + form);
        String user_id = form.getString("user_id");
        String username = form.getString("username");
        String password = form.getString("password");
        int state = form.getIntValue("state");
        String name = form.getString("name");
        String email = form.getString("email");
        String phone = form.getString("phone");
        int role_id = form.getIntValue("role_id");
        boolean flag = adminService.updateUserAllInfo(user_id, password, state, name, email, phone, role_id);

        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(flag));
    }

    //查询用户所有信息
    private void queryUserAllInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = req.getParameter("user_id");
        UserInfo userInfo = adminService.queryUserAllInfo(user_id);

        String string = JSON.toJSONString(userInfo);
        System.out.println("string = " + string);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
    }

    private void queryUserByRoleId(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JSONObject parse = JsonUtils.parse(req);
        System.out.println("parse = " + parse);
        JSONObject roles = parse.getJSONObject("roles");
        boolean role_super = roles.getBooleanValue("role_super");
        boolean role_admin = roles.getBooleanValue("role_admin");
        boolean role_user = roles.getBooleanValue("role_user");
        int currentPage = parse.getIntValue("currentPage");
        List<Integer> roleList = new ArrayList<>();

        if (role_super) roleList.add(1);
        if (role_admin) roleList.add(2);
        if (role_user) roleList.add(3);

        Page<User> userPage = adminService.queryUserByRoleId(currentPage, 10, roleList);
        System.out.println("userPage = " + userPage);
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(userPage));

    }

    private void updateState(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = req.getParameter("user_id");
        String state = req.getParameter("state");

        AdminServiceImpl adminService = new AdminServiceImpl();
        boolean b = adminService.updateState(user_id, Integer.parseInt(state));
        PrintWriter writer = resp.getWriter();
        writer.write(String.valueOf(b));
    }

    //根据科目搜索试题，通过currentTab判断试题类型
    private void searchQuestionBySubject(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //可以考试,解析json
        JSONObject parse = JsonUtils.parse(req);
        System.out.println("parse = " + parse);
        int currentTab = parse.getIntValue("currentTab");//用于判断试题类型
        String subject = parse.getString("subject");//试题科目
        int degree = parse.getIntValue("degree");//试题难度
        int currentPage = parse.getIntValue("currentPage");//当前页

        Page<?> questionPage = null;
        switch (currentTab){//判断考试类型
            case 0 ://单选题
                SingleServiceImpl singleService = new SingleServiceImpl();
                questionPage = singleService.queryExam(subject, degree, currentPage, 10);
                break;
            case 1 ://判断题
                break;
            case 2 ://主观题
                SubjectiveService subjectiveService = new SubjectiveServiceImpl();
                questionPage = subjectiveService.queryByCondition(subject, degree, currentPage, 10);
                break;
        }
        System.out.println("questionPage = " + questionPage);

        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(questionPage));
    }

    //region 题目列表
    private void question_list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentTab = req.getParameter("currentTab");
        String currentPage = req.getParameter("currentPage");
        System.out.println("currentTab = " + currentTab);

        Page<?> list = null;

        switch (currentTab){//判断考试类型
            case "0" ://单选题
                System.out.println("单选题");
                SingleServiceImpl singleService = new SingleServiceImpl();
                list = singleService.query(Integer.parseInt(currentPage), 10);
                break;
            case "1" ://判断题
                System.out.println("判断题");
                break;
            case "2" ://主观题
                System.out.println("主观题");
                SubjectiveService subjectiveService = new SubjectiveServiceImpl();
                list = subjectiveService.query(Integer.parseInt(currentPage), 10);
                break;
        }

        System.out.println("list = " + list);
        String string = JSONArray.toJSONString(list);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
    }
    //endregion

    //region 增加单选题
    private void addSingle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //解析json
        SingleQuestion parse = (SingleQuestion) JsonUtils.parse(req, SingleQuestion.class);
        System.out.println("parse = " + parse);
        SingleServiceImpl singleService = new SingleServiceImpl();

        //判断添加是否成功
        boolean addSingle = singleService.addSingle(parse.getSubject(), parse.getScore(), parse.getQuestion_title(), parse.getQuestion_a(), parse.getQuestion_b(),
                parse.getQuestion_c(), parse.getQuestion_d(), parse.getAnswer(), parse.getDegree());
        PrintWriter writer = resp.getWriter();
        writer.write(String.valueOf(addSingle));
    }
    //endregion

    //region 增加主观题
    private void addJudgment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //解析json
        JudgmentQuestion parse = (JudgmentQuestion) JsonUtils.parse(req, JudgmentQuestion.class);
        System.out.println("parse = " + parse);
        SubjectiveServiceImpl subjectiveService = new SubjectiveServiceImpl();

        //判断添加是否成功
        boolean addSubjective = subjectiveService.addSubjective(parse.getSubject(), parse.getScore(), parse.getQuestion_title(), parse.getAnswer(), parse.getDegree());
        PrintWriter writer = resp.getWriter();
        writer.write(String.valueOf(addSubjective));
    }
    //endregion

    //region 增加主观题
    private void addSubjective(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //解析json
        SubjectiveQuestion parse = (SubjectiveQuestion) JsonUtils.parse(req, SubjectiveQuestion.class);
        System.out.println("parse = " + parse);
        SubjectiveServiceImpl subjectiveService = new SubjectiveServiceImpl();

        //判断添加是否成功
        boolean addSubjective = subjectiveService.addSubjective(parse.getSubject(), parse.getScore(), parse.getQuestion_title(), parse.getAnswer(), parse.getDegree());
        PrintWriter writer = resp.getWriter();
        writer.write(String.valueOf(addSubjective));
    }
    //endregion

    //通过用户名搜索用户
    private void searchUserByName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //解析json
        JSONObject parse = JsonUtils.parse(req);
        String keyWord = parse.getString("keyWord");
        String currentPage = parse.getString("currentPage");
        //System.out.println("keyWord = " + keyWord);
        //System.out.println("currentPage = " + currentPage);

        AdminServiceImpl adminService = new AdminServiceImpl();
        Page<User> searchUserPage = adminService.searchUserByName(keyWord, Integer.parseInt(currentPage), 10);

        String string = JSONArray.toJSONString(searchUserPage);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
    }

    //region 用户列表
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        AdminServiceImpl adminService = new AdminServiceImpl();
        String currentPage = req.getParameter("currentPage");
        System.out.println("currentPage = " + currentPage);
        Page<User> list = adminService.list(Integer.parseInt(currentPage), 10);

        System.out.println("list = " + list);
        String string = JSONArray.toJSONString(list);
        System.out.println("string = " + string);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
    }
    //endregion

    //region 测试axios跨域
    private void axios(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        AdminServiceImpl adminService = new AdminServiceImpl();
        String currentPage = req.getParameter("currentPage");
        System.out.println("currentPage = " + currentPage);
        if (currentPage == null){
            currentPage = "1";
        }
        Page<User> list = adminService.list(Integer.parseInt(currentPage), 10);

        System.out.println("list = " + list);
        String string = JSONArray.toJSONString(list);
        System.out.println("string = " + string);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
    }
    //endregion
*/

}
